package com.todod.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.todod.common.util.SnowflakeIdGenerator;
import com.todod.dto.ShuoMingCatalogDto;
import com.todod.entity.ResultData;
import com.todod.mapper.CatalogManageMapper;
import com.todod.mapper.ConfigMenuMapper;
import com.todod.model.*;
import com.todod.service.ICatalogManageService;
import com.todod.service.IConfigMenuService;
import com.todod.service.IUsersManualFileService;
import com.todod.service.ShuoMingFileService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sxy
 * @date 2025-05-16
 */
@Service
@Transactional
public class ConfigMenuServiceImpl  extends ServiceImpl<ConfigMenuMapper, ConfigMenu>
        implements IConfigMenuService {

    @Resource
    private ConfigMenuMapper configMenuMapper;

    @Resource
    private ShuoMingFileService shuoMingFileService;

    @Resource
    private IUsersManualFileService usersManualFileService;


    @Override
    public List<ConfigMenu> getListByUserIdAndType(Long userId, Long xmId) {
        List<ConfigMenu> returnList = new ArrayList<ConfigMenu>();

        ConfigMenu configMenu = new ConfigMenu();
        configMenu.setId(xmId);
        configMenu.setParentId(-1L);
        configMenu.setSortNum(0);
        configMenu.setMenuName("根节点");
        configMenu.setFlag("ML");
        configMenu.setLink("");
        configMenu.setProjectId(xmId);
        returnList.add(configMenu);

        QueryWrapper<ConfigMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",xmId);
        queryWrapper.eq("project_id",xmId);
        queryWrapper.orderByAsc("sort_num");

        List<ConfigMenu> list = configMenuMapper.selectList(queryWrapper);
        for (ConfigMenu menu : list) {
            menu.setFlag("ML");
            returnList.add(menu);
        }

        return returnList;
    }

    /**
     *
     * @param _userId
     * @param string
     * @param departid
     * @param parentId
     * @return
     */
    public List<ConfigMenu> getListByUserIdAndTypeAndParentId(Long _userId, Long xmId, Long parentId) {
        List<ConfigMenu> returnList = new ArrayList<ConfigMenu>();

        QueryWrapper<ConfigMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id", xmId);
        queryWrapper.eq("parent_id", parentId);
        queryWrapper.orderByAsc("sort_num");

        List<ConfigMenu> list = configMenuMapper.selectList(queryWrapper);
        if (list != null && list.size() > 0) {
            for (ConfigMenu configMenu : list) {
                configMenu.setFlag("ML");
                returnList.add(configMenu);
            }

        }
        // 获取个人创建数据
            List<UsersManualFile> plist = usersManualFileService.getListByClassifyId(parentId);
            for (UsersManualFile usersManualFile : plist) {
                ConfigMenu configMenu = new ConfigMenu();
                configMenu.setId(usersManualFile.getId());
                configMenu.setParentId(parentId);
                configMenu.setSortNum(usersManualFile.getSortNum());
                configMenu.setMenuName(usersManualFile.getTableName());
                configMenu.setFlag("WD");
                configMenu.setProjectId(xmId);

                returnList.add(configMenu);
            }


        return returnList;
    }

    /**
     *
     * @param xiangmuId
     * @return
     */
    public List<ConfigMenu> getListByXmId(Long xiangmuId) {

        QueryWrapper<ConfigMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id", xiangmuId);
        queryWrapper.orderByAsc("sort_num");

        List<ConfigMenu> list = configMenuMapper.selectList(queryWrapper);

        return list;
    }


    /**
     *
     * @param _userId
     * @param configMenu
     */

    public void saveData(Long _userId, ConfigMenu configMenu) {

        configMenu.setId(SnowflakeIdGenerator.getId());

        configMenu.setCreateTime(LocalDateTime.now());
        configMenu.setCreateuserId(_userId);
        configMenuMapper.insert(configMenu);
    }

    /**
     *
     * @param _userId
     * @param configMenu

     */
    public void editData(Long _userId, ConfigMenu configMenu) {

        ConfigMenu configMenuOld = configMenuMapper.selectById(configMenu.getId());

        configMenu.setCreateTime(configMenuOld.getCreateTime());
        configMenu.setCreateuserId(configMenuOld.getCreateuserId());
        configMenu.setModifyTime(LocalDateTime.now());
        configMenu.setModifyuserId(_userId);
        configMenuMapper.updateById(configMenu);
    }


    /**
     *
     * @param code
     * @return
     */
    public Map<String, Object> getInfo(Long code) {
        Map<String, Object> map = new HashMap<>();
        ConfigMenu configMenu = configMenuMapper.getInfo(code);

        map.put("configMenu", configMenu);
        return map;
    }







}
